﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Arroguella.Utilities;
using Arroguella.Entities;

namespace Arroguella.Panels.Popups
{
    /// <summary>
    /// Represents the Player's Inventory menu
    /// </summary>
    public class InventoryMenu : PopupMenu
    {
        #region Constructor

        public static InventoryMenu CreateInventoryMenu(Game game, Rectangle sizeAndShape, Inventory inventory)
        {
            Inventory = inventory;

            return new InventoryMenu(game, sizeAndShape, inventory);
        }

        private InventoryMenu(Game game, Rectangle sizeAndShape, Inventory inventory)
            : base(game, sizeAndShape, InventoryTitle, InventorySubtitle, new List<object>(inventory.EntityNames.Keys), Color.LightSeaGreen, Color.FromNonPremultiplied(70, 70, 80, 225))
        {
            Inventory = inventory;
            InventoryEntities = inventory.EntityNames;
            Color = Color.LightSeaGreen;
            BackgroundColor = Color.FromNonPremultiplied(70, 70, 80, 225);
        }

        #endregion

        #region Initialization and Drawing Methods

        protected override void DrawMenuBody(SpriteBatch spriteBatch)
        {
            // Info section
            DrawInfoSection(spriteBatch);

            // Options
            base.DrawMenuBody(spriteBatch);            
        }

        /// <summary>
        /// The right side of the inventory menu will display detailed information about the highlighted item.
        /// </summary>
        private void DrawInfoSection(SpriteBatch spriteBatch)
        {
            Entity selectedEntity = SelectedEntity;
            string info = selectedEntity != null ? selectedEntity.Description : "No inventory";

            Utils.DrawStringWrappedInRectangle(info, InfoTextRect, Color.White, Font, spriteBatch);
        }

        protected override PopupMenuOption CreateOption(object choice, Rectangle rect)
        {
            string itemName = choice.ToString();

            int quantity = 1;
            Inventory.EntityQuantities.TryGetValue(itemName, out quantity);

            string itemText = string.Format("({0}) {1}", quantity, itemName);

            return base.CreateOption(itemText, rect);
        }

        protected override void CreateBodyRectangle(Rectangle sizeAndShape, Rectangle header)
        {
            BodyRect = new Rectangle(sizeAndShape.X + 5, header.Y + header.Height, sizeAndShape.Width / 2 - 10, sizeAndShape.Height - header.Height);
            CreateInfoRect(sizeAndShape, header);
        }

        protected void CreateInfoRect(Rectangle sizeAndShape, Rectangle header)
        {
            InfoRect = new Rectangle(sizeAndShape.X + 5 + sizeAndShape.Width / 2 + 1, header.Y + header.Height, sizeAndShape.Width / 2 - 10 + 1, sizeAndShape.Height - header.Height);
            InfoTextRect = new Rectangle(InfoRect.X + 5, InfoRect.Y + 5, InfoRect.Height - 10, InfoRect.Width - 10);
        }

        #endregion

        #region Inventory Menu Functionality

        /// <summary>
        /// Simply return the selected Entity.
        /// The caller of this method must handle the equipping properly.
        /// </summary>
        public EquipmentEntity Equip()
        {
            return SelectedEntity as EquipmentEntity;
        }

        #endregion

        #region Properties

        private static Inventory Inventory;

        public Dictionary<string, Entities.Entity> InventoryEntities;

        protected Rectangle InfoRect;

        protected Rectangle InfoTextRect;

        public Entity SelectedEntity { get { return Inventory.EntityNames.Count > 0 ? Inventory.EntityNames[Choices[SelectedOption].ToString()] : null; } }

        private static string InventoryTitle = "Inventory";
        private static string InventorySubtitle = "'E' to equip/use.  'W'/'S' to scroll.  'G' to drop.  'X' to examine/identify.  'I' to close Inventory.";

        #endregion
    }
}
